/**
 * 
 */
package games;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

/**
 * @author Owner
 *
 */
public class ParseData {
	private static final String FOLDER="games\\data\\";
	private static final String SUFFIX=".data";
	private static final String FILE=FOLDER+"data1.xls";
	private static final int FIRST_ROW=1;
	private static final short ID_COLUMN=0;
	private static final short ACTION_COLUMN=1;
	private static final short GAME_NAME_COLUMN=3;
	private static final short PLAYER_NUMBER_COLUMN=4;
	private static final short ENDOWMENT_COLUMN=5;
	private static final short INTERVAL_COLUMN=6;
	private static final short INCREASE_COLUMN=7;
	private static final short GAME_TYPE_COLUMN=8;
	
	//private static final String ULTIMATUM="Ultimatum";
	//private static final String CENTIPEDE="Centipede";
	

	/**
	 * 
	 */
	public ParseData() {
		try {
			POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream(FILE));
			HSSFWorkbook wb = new HSSFWorkbook(fs);
	    HSSFSheet sheet = wb.getSheetAt(0);
	    HSSFRow row;
	    
	    String gameName;
	    deleteFolder();
	    int i=FIRST_ROW;
	    while((row=sheet.getRow(i++))!=null) {
	    	gameName=row.getCell(GAME_NAME_COLUMN).getRichStringCellValue().toString();
	    	if(!gameName.equals("")) addCentipede(row);
	    	else System.out.println(gameName+"hmmm");
	    	
	    	//if(gameName.equals(CENTIPEDE)) addCentipede(row);
	    	//else if(gameName.equals(ULTIMATUM)) addUltimatum(row);
	    }
	    
		} catch(FileNotFoundException e) {
			System.err.println("Can't find the file.");
			e.printStackTrace();
		} catch(IOException e) {
			System.err.println("Problem with excel.");
			e.printStackTrace();
		}
	}
	
	public void addCentipede(HSSFRow row) {
		String game=row.getCell(GAME_NAME_COLUMN).toString().replaceAll(" ","");
    String player=row.getCell(PLAYER_NUMBER_COLUMN).toString().replaceAll(" ","");
    String endowment=row.getCell(ENDOWMENT_COLUMN).toString();
    String interval=row.getCell(INTERVAL_COLUMN).toString();
    String increase=row.getCell(INCREASE_COLUMN).toString();
    String gameType=row.getCell(GAME_TYPE_COLUMN).toString();
		System.out.println(game+player+endowment+increase+gameType);
		
		
		File f = new File(FOLDER+game+player+endowment+interval+increase+gameType+SUFFIX);
    System.out.println(f + (f.exists()? " is found " : " is missing "));
    FileWriter fw;
		try {
			fw=new FileWriter(f,true);
			String id=row.getCell(ID_COLUMN).toString();
			String action=row.getCell(ACTION_COLUMN).toString();
    	fw.write(id+"\t"+action+"\n");
    	fw.flush();
		} catch(IOException e) {
			e.printStackTrace();
		}
    
	}

	public void deleteFolder() {
		File folder=new File(FOLDER);
		System.out.println(folder.isDirectory());
		for(File f:folder.listFiles()) {
			if(f.toString().endsWith(SUFFIX)) f.delete();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new ParseData();
	}

}
